﻿
using System.Windows.Forms;
using Osg;
using ZFGK.Addins;
using ZFGK.OSG.UI;
using ZFGK.OSG.Utility;

namespace OSS.Cmds
{
    public class OssTianTengTestCmd : IExtendCommand
    {
        public CmdResult Execute(ExtendCmdData cmdData, ref string message)
        {

            var viewForm = cmdData.ViewForm as IViewForm;
            if (viewForm == null)
                return CmdResult.Cancel;
            var form = viewForm as Form;
            var osgViewCtrl = viewForm.View as ZfOsgViewCtrl;
            var osgObj = osgViewCtrl.OsgObj;

            Vec3d worldPt1, worldPt2;
            if (!osgViewCtrl.ActionUtility.PickPoint("选择起点", out worldPt1))
            {
                return CmdResult.Cancel;
            }
            if (!osgViewCtrl.ActionUtility.PickPoint("选择终点", out worldPt2))
            {
                return CmdResult.Cancel;
            }

            var speed = 28300;
            var v = worldPt2 - worldPt1;
            var dist = v.length;
            var time = 20;
            var section = 10;
            var port = (1 + section) * section / 2;

            var quat = new Quat(v.Angle2D(), Vec3d.Z_AXIS);

            var animationPath = new AnimationPath();
            animationPath.setLoopMode(AnimationPath.LoopMode.LOOP);

            // 滑行
            animationPath.insert(0, new AnimationPath.ControlPoint(worldPt1, quat));
            for (int i = 1; i < section ; i++)
            {
                animationPath.insert(time / section * 2, new AnimationPath.ControlPoint(worldPt1 + v * ((double)i / port), quat));
            }
            animationPath.insert(time, new AnimationPath.ControlPoint(worldPt2, quat));
            // 起飞
            animationPath.insert(time * 2, new AnimationPath.ControlPoint(worldPt2 + v * 4 + Vec3d.Z_AXIS * 1000, quat));

            var animationPathCallback = new AnimationPathCallback(animationPath, 0, 1);

            var positionAttitudeTransform = new PositionAttitudeTransform();
            positionAttitudeTransform.setUpdateCallback(animationPathCallback);
            positionAttitudeTransform.addChild(OsgDB._.readNodeFile(@"d:\Users\tianteng\Desktop\鄂州飞机动画\737-800-2.ive"));
            positionAttitudeTransform.Name = "1";

            osgViewCtrl.OsgObj.AddOrReplaceModel("动画", positionAttitudeTransform);


            return CmdResult.Succeed;
        }
    }
}
